<?php

namespace App\V3\V3_2_7\Controllers;

use App\Http\Controllers\Controller;
use App\V3\V3_2_7\Services\LocationAnimalService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

class LocationAnimalController extends BaseController
{
    public function outInAnimal(Request $request)
    {
        $time = time();
        $data = $request->all();
        $data['user_id'] = $this->getUserId();
        $data['main_id'] = $this->getMainUserId();
        $this->operateLog('宠物出入库', $data, $time);
        if(!empty($data['in_out_type'])){
            if(!in_array((int)$data['in_out_type'], [1, 2], true)){
                return $this->fail('','出入库类型，有误');
            }
        }else{
            return $this->fail('','出入库类型，不能为空');
        }
        return Cache::lock(__CLASS__ . __FUNCTION__ . $data['user_id'], 5)
            ->get(function () use ($data, $time) {
                return LocationAnimalService::outInAnimal($data, $time);
            });
    }

    public function update(Request $request)
    {
        $time = time();
        $data = $request->all();
        $data['user_id'] = $this->getUserId();
        $data['main_id'] = $this->getMainUserId();
        $this->operateLog('修改宠物信息', $data, $time);
        return Cache::lock(__CLASS__ . __FUNCTION__ . $data['user_id'], 5)
            ->get(function () use ($data, $time) {
                return LocationAnimalService::update($data, $time);
            });
    }

}
